package com.xbjtech.service.impl;

import java.net.URLEncoder;
import java.util.Map;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.xbjtech.service.SmsService;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class SmsServiceImpl implements SmsService {

	@Override
	public void sendMsg(String smsModelId, String mobile, Map<String, String> params) {
		HttpClient client = new HttpClient();
		PostMethod post = new PostMethod("http://api.sms.cn/sms/");
		try {
			post.addRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
			NameValuePair[] data = { 
					new NameValuePair("ac", "send"), 
					new NameValuePair("uid", "leejun"),
					new NameValuePair("pwd", "3fae76d636a0adeb597b41d31959624e"), 
					new NameValuePair("mobile", mobile),
					new NameValuePair("template", smsModelId),
					new NameValuePair("content", URLEncoder.encode(new Gson().toJson(params), "UTF-8"))};
			post.setRequestBody(data);
			client.executeMethod(post);
			String result = new String(post.getResponseBodyAsString().getBytes("utf-8"));
			log.info(result);
		} catch (Exception e) {
			log.info("SmsServiceImpl.send()-->" + e.getMessage());
		} finally {
			post.releaseConnection();
		}
	}
}
